#include "Prop.h"
#include <stdlib.h>

prop *CreateProp(enum PropType type, void *ptr)
{
    prop *p = (prop *)malloc(sizeof(prop));
    if(p == NULL)
        return NULL;
    p->type = type;
    p->ptr = ptr;
    return p;
}

void FreeProp(void *p, void (*clearPtr)(void *))
{
    prop *pro = (prop*)p;
    clearPtr(pro->ptr);
    free(pro);
}

weapon *CreateWeapon(const char *name, int attack,int crit,enum Effect effect)
{
    weapon *w = (weapon *)malloc(sizeof(weapon));
    if(w == NULL)
        return NULL;
    w->attack = attack;
    w->crit = crit;
    w->effect = effect;
    w->name =  GetString(name);
    return w;
}

void FreeWeapon(void *ptr)
{
    weapon * w = (weapon *)ptr;
    ClearString(&w->name);
    free(w);
}

MyStr WeaponToString(weapon *w)
{
    char str[1024] = {0};
    sprintf(str,"%d %s %d %d %d\n",Weapon,w->name.string,w->attack,w->crit,w->effect);
    return GetString(str);
}

defence *CreateDefence(const char *name,int def,int parry,enum Effect effect)
{
    defence * d = (defence*)malloc(sizeof(defence));
    if(d == NULL)
    {
        return NULL;
    }
    d->def = def;
    d->effect = effect;
    d->parry = parry;
    d->name =  GetString(name);

    return d;
}

void FreeDefence(void *ptr)
{
    defence *d = (defence *)ptr;
    ClearString(&d->name);
    free(d);
}

MyStr DefenceToString(defence *d)
{
    char str[1024] = {0};
    sprintf(str,"%d %s %d %d %d\n",Defence,d->name.string,d->def,d->parry,d->effect);
    return GetString(str);
}

medicament *CreateMedicament(const char *name, int hp, int attack, int defence,int crit,int parry,enum Effect effect)
{

    medicament * m = (medicament *)malloc(sizeof(medicament));
    if(m == NULL)
        return NULL;
    m->hp =hp;
    m->attack = attack;
    m->def = defence;
    m->crit = crit;
    m->parry = parry;
    m->effect = effect;
    m->name = GetString(name);
    return m;
}

void FreeMedicament(void *ptr)
{
    medicament *m = (medicament *)ptr;
    ClearString(&m->name);
    free(m);
}

MyStr MedicamentToString(medicament *m)
{
    char str[1024] = {0};
    sprintf(str,"%d %s %d %d %d %d %d %d\n",Medicament,m->name.string,m->hp,m->attack,m->def,m->crit,m->parry,m->effect);
    return GetString(str);
}
